home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / UUCICO / ULIBIP.C < prev    next >
C/C++ Source or Header  |  1993-10-02  |  27KB  |  814 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       u l i b i p . c                                              */
  3. /*                                                                    */
  4. /*       TCP/IP port communications driver for Windows sockets        */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Copyright (c) David M. Watt 1993, All Rights Reserved           */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  13. /*    Wonderworks.                                                    */
  14. /*                                                                    */
  15. /*    All rights reserved except those explicitly granted by the      */
  16. /*    UUPC/extended license agreement.                                */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                          RCS Information                           */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. /*
  24.  *    $Id: ulibip.c 1.6 1993/10/02 23:12:35 dmwatt Exp $
  25.  *
  26.  *    $Log: ulibip.c $
  27.  * Revision 1.6  1993/10/02  23:12:35  dmwatt
  28.  * Winsock error message support
  29.  *
  30.  * Revision 1.5  1993/09/26  03:32:27  dmwatt
  31.  * Use Standard Windows NT error message module
  32.  *
  33.  * Revision 1.4  1993/09/25  03:07:56  ahd
  34.  * Addition error traps by Dave Watt
  35.  *
  36.  * Revision 1.3  1993/09/23  03:26:51  ahd
  37.  * Correct setting of carrier detect
  38.  *
  39.  * Revision 1.2  1993/09/21  01:42:13  ahd
  40.  * Use standard MAXPACK limit for save buffer size
  41.  *
  42.  * Revision 1.1  1993/09/20  04:48:25  ahd
  43.  * Initial revision
  44.  *
  45.  */
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*                        System include files                        */
  49. /*--------------------------------------------------------------------*/
  50.  
  51. #include <windows.h>
  52. #include "winsock.h"
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <time.h>
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                    UUPC/extended include files                     */
  61. /*--------------------------------------------------------------------*/
  62.  
  63. #include "lib.h"
  64. #include "hlib.h"
  65. #include "ulib.h"
  66. #include "comm.h"          // Modem status bits
  67. #include "ssleep.h"
  68. #include "catcher.h"
  69.  
  70. #include "commlib.h"       // Trace functions, etc.
  71. #include "pwserr.h"        // Windows sockets error messages
  72.  
  73. #ifdef _Windows
  74. #include "pwinsock.h"      // definitions for 16 bit Winsock functions
  75. #endif
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*                        Internal prototypes                         */
  79. /*--------------------------------------------------------------------*/
  80.  
  81. void AtWinsockExit(void);
  82. boolean IsFatalSocketError(int err);
  83. void tcloseline(void);
  84.  
  85. /*--------------------------------------------------------------------*/
  86. /*                          Global variables                          */
  87. /*--------------------------------------------------------------------*/
  88.  
  89. static boolean carrierDetect = FALSE;
  90.  
  91. currentfile();
  92. static boolean hangupNeeded = TRUE;
  93. extern boolean winsockActive;                   // Initialized in catcher.c
  94. static SOCKET pollingSock = INVALID_SOCKET;     // The current polling socket
  95. static SOCKET connectedSock = INVALID_SOCKET;   // The currently connected socket
  96. static boolean connectionDied = FALSE;          // The current connection failed
  97.  
  98. /*--------------------------------------------------------------------*/
  99. /*                           Local defines                            */
  100. /*--------------------------------------------------------------------*/
  101.  
  102. #ifndef MAKEWORD
  103. #define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8))
  104. #endif
  105.  
  106. /*--------------------------------------------------------------------*/
  107. /*    I n i t W i n s o c k                                           */
  108. /*                                                                    */
  109. /*    Start the Windows sockets DLL                                   */
  110. /*--------------------------------------------------------------------*/
  111.  
  112. boolean InitWinsock(void)
  113. {
  114.    WSADATA WSAData;
  115.    int status;
  116.    static boolean firstPass = TRUE;
  117.  
  118.    if ( winsockActive )
  119.       return TRUE;
  120.  
  121. /*--------------------------------------------------------------------*/
  122. /*       The atexit() must precede the WSAStartup() so the            */
  123. /*       FreeLibrary() call gets done                                 */
  124. /*--------------------------------------------------------------------*/
  125.  
  126.    if ( firstPass )
  127.    {
  128.       firstPass = FALSE;
  129.       atexit(AtWinsockExit);
  130.    }
  131.  
  132. #ifdef _Windows
  133.    if (!pWinSockInit())
  134.       return FALSE;
  135. #endif
  136.  
  137. // status = WSAStartup(MAKEWORD(1,1), &WSAData);
  138.    status = WSAStartup(0x0101, &WSAData);
  139.  
  140.    if (status != 0)
  141.    {
  142.       printf("WSAStartup Error: %d", status);
  143.       return FALSE;
  144.    }
  145.  
  146.    winsockActive = TRUE;
  147.    return TRUE;
  148.  
  149. } /* InitWinsock */
  150.  
  151. /*--------------------------------------------------------------------*/
  152. /*       A t W i n s o c k E x i t                                    */
  153. /*                                                                    */
  154. /*       Clean up Windows DLL at shutdown                             */
  155. /*--------------------------------------------------------------------*/
  156.  
  157.  
  158. void AtWinsockExit(void)
  159. {
  160.    WSACleanup();
  161.  
  162. #ifdef _Windows
  163.    pWinSockExit();
  164. #endif
  165.  
  166.    winsockActive = FALSE;
  167.  
  168. }  /* AtWinsockExit */
  169.  
  170. /*--------------------------------------------------------------------*/
  171. /*    t o p e n a c t i v e                                           */
  172. /*                                                                    */
  173. /*    Open an active socket connection for I/O                        */
  174. /*--------------------------------------------------------------------*/
  175.  
  176. #ifdef __TURBOC__
  177. #pragma argsused
  178. #endif
  179.  
  180. int tactiveopenline(char *name, BPS bps, const boolean direct)
  181. {
  182.    SOCKADDR_IN sin;
  183.    LPHOSTENT phe;
  184.    LPSERVENT pse;
  185.  
  186.    if (!InitWinsock())           // Initialize library?
  187.       return TRUE;               // No --> Report error
  188.  
  189.    if (portActive)              /* Was the port already active?      */
  190.       closeline();               /* Yes --> Shutdown it before open  */
  191.  
  192.    printmsg(15, "tactiveopenline: %s", name);
  193.  
  194.    norecovery = FALSE;     // Flag we need a graceful shutdown after
  195.                            // Ctrl-BREAK
  196.  
  197.    carrierDetect = FALSE;  /* No modem connected yet                */
  198.  
  199.    connectionDied = FALSE; /* The connection hasn't failed yet */
  200.  
  201. /*--------------------------------------------------------------------*/
  202. /*                        Get remote host name                        */
  203. /*--------------------------------------------------------------------*/
  204.  
  205.    sin.sin_family = AF_INET;
  206.    phe = gethostbyname(name);
  207.  
  208.    if (phe)
  209. #ifdef _Windows
  210.       _fmemcpy((char FAR *) &(sin.sin_addr),
  211.                (char FAR *) phe->h_addr,
  212.                phe->h_length);
  213. #else
  214.       memcpy((char FAR *) &(sin.sin_addr),
  215.              (char FAR *) phe->h_addr,
  216.              phe->h_length);
  217. #endif
  218.    else {
  219.       sin.sin_addr.s_addr = inet_addr(name);
  220.  
  221.       if ( sin.sin_addr.s_addr == INADDR_NONE )
  222.       {
  223.          int wsErr = WSAGetLastError();
  224.  
  225.          printmsg(0, "tactiveopenline: "
  226.             "Is '%s' listed in the hosts file or a valid IP address?",
  227.             name);
  228.          printWSerror("gethostbyname", wsErr);
  229.          return TRUE;
  230.       }
  231.  
  232.    } /* else */
  233.  
  234. /*--------------------------------------------------------------------*/
  235. /*                     Get the TCP/IP port number                     */
  236. /